package com.flycent.hqhs.ui;

import java.lang.ref.WeakReference;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;

import com.flycent.hqhs.App;
import com.flycent.hqhs.R;
import com.flycent.hqhs.http.WebClient;
import com.flycent.hqhs.util.StringUtil;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 *  重置密码
 * @author fbl
 */
public class ResetPwdActivity extends BackableTitleBarActivity{
	@ViewInject(id = R.id.reset_userName_editText) TextView mResetname;
	@ViewInject(id = R.id.reset_userpwd_editText) EditText mResetPwd;
	@ViewInject(id = R.id.reset_userpwd_editText_confirm) TextView mResetPwd2;
	@ViewInject(id = R.id.reset_code_editText) EditText mResetCode;
	@ViewInject(id = R.id.reset_sure , click = "butClick") Button mSureBut;
	private String mUserNameTxt;
	private String codeStr;
	private Handler mHandler;

	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_reset_pwd);
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle(R.string.title_reset_pwd);
		
		mHandler = new MyHandler(this);
		
		mUserNameTxt = getIntent().getStringExtra("userName");
		codeStr = getIntent().getStringExtra("code");
		if(mUserNameTxt != null && !"".equals(mUserNameTxt))
			mResetname.setText(mUserNameTxt);
	}

	public void butClick(View v){
		if( v == mSureBut){
			doEditTextVerify();
		}
	}

	private void doEditTextVerify() {
		if( invalidateText(mResetPwd,R.string.info_new_pwd_donot_empty) ){
			return;
		}
		if( !(StringUtil.isPhoneNo(getText(mResetname)) 
				|| StringUtil.isEmail(getText(mResetname)) ) ){
			App.showShortToast(getString(R.string.txt_info_true_username_no));
			return;
		}
		// 两次密码不一致
		if (!getText(mResetPwd).equals(getText(mResetPwd2))) {
			App.showShortToast(getString(R.string.info_new_confirm_pwd_donot_match));
			return;
		}
		doResetPassWord();

	}

	private void doResetPassWord() {
		showLoadingDialog("");

		new Thread(){

			public void run() {
				try {
					WebClient.getInstance(ResetPwdActivity.this).passwordReset(getText(mResetname),codeStr, getText(mResetPwd));
					mHandler.sendEmptyMessage(0);
				} catch (Exception e) {
					e.printStackTrace();
					mHandler.sendEmptyMessage(1);
				}
			};

		}.start();

	}
	
	@SuppressLint("HandlerLeak") 
	private class MyHandler extends Handler{
		
		private WeakReference<Activity> mActivity;
		
		private MyHandler(Activity activity){
			mActivity = new WeakReference<Activity>(activity);
		}
		
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			if( mActivity.get() != null ){
				switch (msg.what) {
				case 0:
					//修改成功
					dismissLoadingDialog();
					App.showShortToast("修改成功");
					finish();
					GetCodeActivity.instance.finish();
					ForgotPwdActivity.instance.finish();

					break;
				case 1:
					//修改失败
					dismissLoadingDialog();
					break;
				case 2:
					mResetname.setError(getString(R.string.info_please_input_user_is_no_exist));
					break;
				default:
					break;
				}
				
				
			}
			
			
		}
		
	}

}
